/*
 * TypeAst.cpp
 *
 *  Created on: 5 Nov 2010
 *      Author: kn308
 */

#include <sstream>
#include <llvm/TypeSymbolTable.h>

#include <MAlice/Ast/TypeAst.h>
#include <MAlice/Ast/ErrorAst.h>
#include <MAlice/Codegen.h>
#include <MAlice/Parser/location.hh>



namespace MAlice
{

TypeAst::TypeAst(location loc, const string& name) : Ast(loc),
	mName(name)
{
	MALICE_TRACE;
}

TypeAst::~TypeAst()
{
	MALICE_TRACE;
}

const llvm::Type* TypeAst::type()
{
	Codegen* g  = Codegen::get();
	Module* mod = g->getModule();
	TypeSymbolTable& syms = mod->getTypeSymbolTable();
	TypeSymbolTable::const_iterator i = syms.find(StringRef(mName));
	if (i == syms.end())
	{
		stringstream ss;
		ss << '\'' << mName << '\'' << " is not a defined type";
		throw runtime_error(ss.str());
	}
	else
	{
		return i->second;
	}
}

llvm::Value* TypeAst::generate()
{
	MALICE_TRACE;
	throw runtime_error("TypeAst::generate() is not callable!");
}

}
